package cn.zbzcode.mq._05topic;

import cn.zbzcode.mq.ConnectionUtil;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Sen {

    //交换机名字
    public static final String EXCHANGE_NAME_TOPIC = "exchange_name_topic";

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try{
            //1.通过连接工具创建连接
            connection = ConnectionUtil.getConnection();

            //2.通过连接对象,创建通道
            channel = connection.createChannel();

            //3.通过通道,声明交换机
            //参数: 交换机名字, 交换机类型
            channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, BuiltinExchangeType.TOPIC );

            //4.定义消息内容
            String msg = "无敌也是一种淡淡的忧伤";

            //5.通过通道发送消息
            //参数:交换机(""默认) , 队列的routingkey(理解为队列名字),其他属性,消息内容
            channel.basicPublish(EXCHANGE_NAME_TOPIC, "add.name.first" ,null ,msg.getBytes() );
            System.out.println("已经发送消息:msg："+msg);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(channel != null){
                try {
                    channel.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
